﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using AjaxControlToolkit;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;

[assembly: System.Web.UI.WebResource("OpenPhotoAlbum.Controls.TabPanels.js", "text/javascript")]

namespace OpenPhotoAlbum.Controls
{
	[ClientScriptResource("OpenPhotoAlbum.Controls.TabPanels", "OpenPhotoAlbum.Controls.TabPanels.js")]
	public class TabPanels : ScriptControlBase
	{
		private const string DEFAULT_CSS_CLASS = "tabPanels";
		private const string VSK_VISIBLE_INDEX = "vi";

		public TabPanels()
			: base(HtmlTextWriterTag.Div)
		{
			TabPanelItems = new List<TabPanelItem>();
			CssClass = DEFAULT_CSS_CLASS;
		}

		public List<TabPanelItem> TabPanelItems { get; set; }

		[ExtenderControlProperty]
		public string TabPanelItemList
		{
			get
			{
				JavaScriptSerializer jss = new JavaScriptSerializer();
				return jss.Serialize(TabPanelItems);
			}
		}

		public void SetVisibleIndex(int index)
		{
			for (int i = 0; i < TabPanelItems.Count; i++)
			{
				TabPanelItem item = TabPanelItems[i];

				if (i == index) { item.Visible = true; }
				else { item.Visible = false; }
			}
			ViewState[VSK_VISIBLE_INDEX] = index;
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (this.Page.IsPostBack && ViewState[VSK_VISIBLE_INDEX] != null)
			{
				SetVisibleIndex((int)ViewState[VSK_VISIBLE_INDEX]);
			}
		}
	}

	public class TabPanelItem
	{
		public string Name { get; set; }
		public string PanelClientId { get; set; }
		public bool Visible { get; set; }
	}
}
